/*
 *  Copyright (C) 2011 felixtheripper <felixtheripper_AT_gmail.com>
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package org.myorg.shr.gui.swg.util.logging;

import java.io.File;
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.SimpleFormatter;

/**
 *
 * @author felixtheripper
 *
 * email: felixtheripper_AT_gmail.com
 * ... please remove "_AT_" from the above string to get the right email address
 *
 */

public class FileHandlerShr extends FileHandler
{


public static void s_initFileHandler(String[] strsLogsParentFolder, String strVersionTransformed){
        FileHandlerShr.STRS_LOGS_PARENT_FOLDERS = strsLogsParentFolder;
        FileHandlerShr.STR_APPLI_VERSION_TRANSFORMED = strVersionTransformed;



        File fleVersion = null;

        try
        {
            fleVersion = s_getOrCreateVersionDir();
        }
        catch (Exception e)
        {
            // TODO should exit, memo: tbrl for log file path assignment
            e.printStackTrace();
        }

        if (fleVersion != null)
            FileHandlerShr._STR_PATH_LOG_DIR_PARENT_ = fleVersion.getAbsolutePath();



    }
	static public String s_getPathAbsFileLogJava()
	{
		return
			FileHandlerShr._STR_PATH_LOG_DIR_PARENT_  +
			java.io.File.separator +
			FileHandlerShr._F_STR_NAMELOGJAVA;
	}



	static public FileHandlerShr s_getInstance()
	{
            if (FileHandlerShr._INSTANCE == null)
            {
                    try
                    {
                            FileHandlerShr._INSTANCE = new FileHandlerShr();
                    }

                    catch(Exception exc)
                    {
                            exc.printStackTrace();
                    }
            }

            return FileHandlerShr._INSTANCE;
	}

	// trick
	protected FileHandlerShr()
	    throws
	        IOException,
		    SecurityException
	{
            super(FileHandlerShr.s_getPathAbsFileLogJava());
            super.setFormatter(new SimpleFormatter());
	}


    static public File s_getOrCreateVersionDir() throws
		Exception
	{
		File fleParent = new File(System.getProperty("user.home"));

		if (fleParent == null)
		{
			// error
			return null;
		}

		if (! fleParent.isDirectory())
		{
			// error
			return null;
		}

		if (!fleParent.canRead() || !fleParent.canWrite())
		{
			fleParent = new File("."); // use install dir instead!
		}

		File fleCur = null;

        //PrpNamingEtk.s_init();

		for (int i=0; i<FileHandlerShr.STRS_LOGS_PARENT_FOLDERS.length-1; i++)
		{
			fleCur = new File(fleParent, FileHandlerShr.STRS_LOGS_PARENT_FOLDERS[i]);

			if (fleCur == null)
			{
				// error
				return null;
			}

			if (! fleCur.exists())
			{
				fleCur.mkdir();
			}

			else if (! fleCur.isDirectory())
			{
                //error
				return null;
			}

			fleParent = fleCur;
		}

		fleCur = new File(fleParent, FileHandlerShr.STR_APPLI_VERSION_TRANSFORMED);

		if (! fleCur.exists())
		{
			fleCur.mkdir();
		}

		else if (! fleCur.isDirectory())
		{
            //error
			return null;
		}

		return fleCur;
	}
	static public File s_getVersionDir() throws
		Exception
	{
		File fleParent = new File(System.getProperty("user.home"));

		if (fleParent == null)
		{
			throw new Exception("Failed to get user.home System's property");
		}

		if (! fleParent.isDirectory())
		{
			throw new Exception("Not a directory: " + fleParent.getAbsolutePath());
		}

		if (!fleParent.canRead() || !fleParent.canWrite())
		{
			throw new Exception("Cannot read or/and write: " + fleParent.getAbsolutePath());
		}

		File fleCur = null;

		for (int i=0; i<FileHandlerShr.STRS_LOGS_PARENT_FOLDERS.length-1; i++)
		{
			fleCur = new File(fleParent, FileHandlerShr.STRS_LOGS_PARENT_FOLDERS[i]);

			if (fleCur == null)
			{
				throw new Exception("Got nil file: " + "PropNamingEtk.STRS_LOGS_PARENT_FOLDERS[" + i + "]");
			}

			if (! fleCur.exists())
			{
				throw new Exception("file does not exist: " + fleCur.getAbsolutePath());
			}

			if (! fleCur.isDirectory())
			{
				throw new Exception("file dis not a directory: " + fleCur.getAbsolutePath());
			}

			fleParent = fleCur;
		}


		fleCur = new File(fleParent, FileHandlerShr.STR_APPLI_VERSION_TRANSFORMED);

		if (fleCur == null)
		{
			throw new Exception("Got nil file: " + "PropNamingEtk.STR_APPLI_VERSION_TRANSFORMED");
		}

		if (! fleCur.exists())
		{
			throw new Exception("file does not exist: " + fleCur.getAbsolutePath());
		}

		if (! fleCur.isDirectory())
		{
			throw new Exception("file dis not a directory: " + fleCur.getAbsolutePath());
		}

		return fleCur;
	}

    private static String[] STRS_LOGS_PARENT_FOLDERS ;
    static private String STR_APPLI_VERSION_TRANSFORMED;

    final static private String _F_STR_NAMELOGJAVA = "sessionJava.log";

	static protected String _STR_PATH_LOG_DIR_PARENT_ = null;

	static private FileHandlerShr _INSTANCE = null;
    
}

